import os
import cv2
import utils
import numpy as np
from tqdm import tqdm


SrcPath = r"J:\Temp\1219\M"
DstPath = r'J:\Temp\1221\M'

NameNum = 0
for root, dirs, files in os.walk(SrcPath):
    for file in tqdm(files):
        if file[-3:] == 'jpg':
            img = cv2.imread(root + '\\' + file)
            Infos = utils.ReadXml(root + '\\' + file[:-3] + 'xml')
            NameNum += 1
            NewInfos = [DstPath + '\\%05d.xml' % NameNum]
            for info in Infos:
                if info[0] > 600:
                    info[0] -= 600
                    info[1] += 300
                    info[2] -= 600
                    info[3] += 300
                NewInfos.append(info)
            NewImg = np.zeros((600, 600, 3), dtype=np.uint8)
            NewImg[:300, :] = img[:, :600]
            NewImg[300:, :] = img[:, 600:]
            cv2.imwrite(NewInfos[0][:-3] + 'jpg', NewImg)
            utils.WriteXml(NewInfos, 600, 600)
